import { inject, onMounted } from "vue"
import { mapStore } from "@/store"
export const addNotice = () => {
    let map
    let i = 0
    const store = mapStore()
    onMounted(() => {
        map = inject('$map')
    })

    const okNotice = () => {
        store.noticeLists.push({
            type: store.textType,
            detail: store.textDetail,
            date: store.textDate,
            dateNow: store.textDateNow,
        })
        store.isNotice = false
        const totalDate = getDate(i)
        const nowDate = getNow(i)
        store.noticeLists[i].date = totalDate
        store.noticeLists[i].dateNow = nowDate
        i++
        store.total = store.noticeLists.length
        if (store.noticeLists.length <= 5) {
            store.currentData = store.noticeLists.slice(store.offset_, store.offset_ + 5)
        }
    }

    // 获取年月日
    function getDate(index) {
        const dateArray = store.noticeLists[index].date.toString().split(' ')
        const year = dateArray[3]
        let month = dateArray[1]
        switch (month) {
            case 'Jan':
                month = 1
                break;
            case 'Feb':
                month = 2
                break;
            case 'Mar':
                month = 3
                break;
            case 'Apr':
                month = 4
                break;
            case 'May':
                month = 5
                break;
            case 'Jun':
                month = 6
                break;
            case 'Jul':
                month = 7
                break;
            case 'Aug':
                month = 8
                break;
            case 'Sept':
                month = 9
                break;
            case 'Oct':
                month = 10
                break;
            case 'Nov':
                month = 11
                break;
            default:
                month = 12
        }
        const day = dateArray[2]
        const totalDate = year + "年" + month + "月" + day + "日"
        return totalDate
    }
    // 获取当前时间
    function getNow(index) {
        const nowArray = store.noticeLists[index].dateNow.toString().split(' ')
        const nowDate = nowArray[4]
        return nowDate
    }

    const canelNotice = () => {
        store.isNotice = false
    }

    return {
        okNotice,
        canelNotice,
    }
}